/*
 * Copyright (c) 2009, Hamish Morgan. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the University of Sussex nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package piccat.model;

import java.net.URL;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;

/**
 * Class that holds information about a single picture.
 * 
 * @author Hamish Morgan
 */
public class Picture implements Cloneable {

    public static final int NO_OWNER = -1;
    //
    private final URL url;
    private final String type;
    private boolean correct;
    private int ownerId;
    /**
     * Hold all listeners to this class
     */
    private final EventListenerList listenerList = new EventListenerList();
    /**
     * The change event is always the same, so create it lazily and cache it
     * for later use.
     */
    private ChangeEvent changeEvent = null;

    public Picture(URL image, String type) {
        this.url = image;
        this.type = type;
        correct = false;
        this.ownerId = NO_OWNER;

    }

    public Picture(Picture other) {
        this.url = other.url;
        this.type = other.type;
        this.correct = other.correct;
        this.ownerId = other.ownerId;
    }

    public URL getUrl() {
        return url;
    }

    public String getType() {
        return type;
    }

    public boolean isCorrect() {
        return correct;
    }

    public void setCorrect(boolean correct) {
        if (this.correct != correct) {
            this.correct = correct;
            fireStateChangedEvent();
        }
    }

    public int getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(int ownerId) {
        if (ownerId != this.ownerId) {
            this.ownerId = ownerId;
            fireStateChangedEvent();
        }
    }

    public boolean matches(Picture other) {
        if (other == null) return false;
        if (this.url != other.url &&
                (this.url == null || !this.url.equals(other.url)))
            return false;
        else
            return true;
    }

    public boolean equals(Picture other) {
        if (other == null) return false;
        if (this.url != other.url &&
                (this.url == null || !this.url.equals(other.url)))
            return false;
        if ((this.type == null) ? (other.type != null)
                : !this.type.equals(other.type)) return false;
        if (this.ownerId != other.ownerId) return false;
        return true;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final Picture other = (Picture) obj;
        if (this.url != other.url &&
                (this.url == null || !this.url.equals(other.url)))
            return false;
        if ((this.type == null) ? (other.type != null)
                : !this.type.equals(other.type)) return false;
        if (this.ownerId != other.ownerId) return false;
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 13 * hash + (this.url != null ? this.url.hashCode() : 0);
        hash = 13 * hash + (this.type != null ? this.type.hashCode() : 0);
        hash = 13 * hash + this.ownerId;
        return hash;
    }

    @Override
    public String toString() {
        return url.toString();
    }

    @Override
    public Picture clone() {
        return new Picture(this);
    }

    // ===================================================================
    public void addChangeListener(ChangeListener listener) {
        listenerList.add(ChangeListener.class, listener);
    }

    public void removeChangeListener(ChangeListener listener) {
        listenerList.remove(ChangeListener.class, listener);
    }

    public ChangeListener[] getChangeListeners() {
        return listenerList.getListeners(ChangeListener.class);
    }

    public int getChangeListenerCount() {
        return listenerList.getListenerCount(ChangeListener.class);
    }

    protected void fireStateChangedEvent() {
        final Object[] listeners = listenerList.getListenerList();
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == ChangeListener.class) {
                if (changeEvent == null)
                    changeEvent = new ChangeEvent(this);
                ((ChangeListener) listeners[i + 1]).stateChanged(changeEvent);
            }
        }
    }
}
